<template>
	<view class="fun-flex-col page">
		<view class="fun-flex-col group">
			<text class="fun-self-start font text_3 text_4">Enter your Top Up Amount($)</text>
			
			<view class="fun-flex-col group_2">
				<view class="fun-flex-row fun-justify-between fun-items-center fun-self-stretch group_3">
					<input class="font_3 text-wrapper" v-model="amount" placeholder="Enter your Top Up Amount" type='digit'/>
					<text class="font_3">USD</text>
				</view>
				<text class="fun-self-start text_6 mt-13">The minimum top up amount is {{rechargeInfo.minimum}} USD.</text>
			</view>
			
			<view class="fun-flex-col group_4">
				<text class="fun-self-start font">Select a Payment Tool</text>
				<text class="fun-self-start text_8">Select from crypto wallet or bank card transfer.</text>
				<view class="fun-flex-row fun-justify-between equal-division">
					<view class="fun-flex-col fun-items-center fun-justify-between equal-division-item" :class="payment == 1 ? 'current' : ''" @click="payment = 1">
						<image class="image_4" src="@/static/account/walllet_active.png" v-if='payment == 1'/>
						<image class="image_4" src="@/static/account/walllet.png" v-else/>
						<text class="text_9">Crypto Wallet QR Code</text>
					</view>
					
					<!-- <view class="fun-flex-col fun-items-center fun-justify-between equal-division-item" :class="payment == 2 ? 'current' : ''" @click="payment = 2">
						<image class="image_4" src="@/static/account/walllet_active.png" v-if='payment == 2'/>
						<image class="image_4" src="@/static/account/walllet.png" v-else/>
						<text class="text_9" >Bank card transfers</text>
					</view> -->
				</view>
				
				<text class="fun-self-start font text_11">Select a protocal</text>
				<view class="fun-flex-row  fun-justify-between group_5">
					<view class="protocal"
						v-for="item in methodsList"
						:key="item.id"
						:class="protocal == item.id? 'active' : ''"
						@click="changeMethods(item)"
					>
						{{item.name}}
					</view>
				</view>
			</view>
			
			<view class="fun-flex-col group_6">
				<text class="fun-self-start font_3 text_3 text_14">transaction fee</text>
				<view class="fun-flex-row fun-justify-between fun-items-baseline group_7">
					<!-- <input class="font_3 text-wrapper" placeholder="Enter transaction fee" type='digit'/> -->
					<text class="font_3 text-wrapper">{{feeAmount}}</text>
					<text class="font_3">USD</text>
				</view>
				<text class="fun-self-start font_4 text_16">The transaction fee rate is {{rechargeInfo.serviceCharge}}%.</text>
			</view>
			
			<view class="text-wrapper_4" @click="confirm">Confirm</view>
		</view>
	</view>
</template>

<script>
	import {recharge,rechargeMethods} from "@/api/account.js"
	import {roundToDecimal} from "@/common/js/func.js"
	export default {
		data() {
			return {
				amount:"",
				methodsList:[],
				rechargeInfo:{},
				payment:1,
				protocal:1
			};
		},
		
		onLoad() {
			this.getRechargeMethods()
		},
		
		computed:{
			feeAmount(){
				return roundToDecimal(this.amount * this.rechargeInfo.serviceCharge / 100)
			}
		},

		methods: {
			getRechargeMethods(){
				rechargeMethods().then(res => {
					if(res.code == 0){
						this.methodsList = res.data || [];
						this.rechargeInfo = this.methodsList?.length ? this.methodsList[0] : {};
						this.protocal = this.rechargeInfo?.id
					}
					else{
						this.UTILS.logStr(res.msg || "Failed to retrieve data")
					}
				})
			},
			
			changeMethods(item){
				this.protocal = item.id;
				this.rechargeInfo = Object.assign({},item);
			},
			
			confirm(){
				if(!this.amount){
					this.UTILS.logStr("Enter your Top Up Amount");
					return
				}
				if(Number(this.amount) < Number(this.rechargeInfo.minimum)){
					this.UTILS.logStr(`Not less than${this.rechargeInfo.minimum}USD`);
					return
				}
				const params = {
					methodId:this.rechargeInfo.id,
					amount:Number(this.amount)
				}
				recharge(params).then(res => {
					if(res.code == 0){
						this.UTILS.logStr(res.msg || "recharge Success");
						uni.navigateTo({
							url:'/pages/account/scanCode?orderId='+encodeURIComponent(res?.data?.id),
							animationDuration: 2000
						})
					}
					else{
						this.UTILS.logStr(res.msg || "Failed to retrieve data")
					}
				})
				
			}
		},
	};
</script>

<style scoped lang="scss">
	.ml-5 {
		margin-left: 10rpx;
	}

	.mt-13 {
		margin-top: 26rpx;
	}

	.ml-17 {
		margin-left: 34rpx;
	}

	.mt-7 {
		margin-top: 14rpx;
	}

	.page {
		padding: 32rpx;
		background-color: #ffffff;
		width: 100%;
		overflow-y: auto;
		overflow-x: hidden;
		height: 100%;
		.group {
			.text_3 {
				text-transform: capitalize;
			}
			.group_2 {
				margin-top: 36rpx;
				.group_3 {
					border-bottom: solid 2rpx #1a7b99;
					.text-wrapper {
						flex: 1;
						padding: 8rpx 0;
						color: #000000;
					}
				}

				.text_6 {
					color: #777e90;
					font-size: 28rpx;
					font-family: SF Pro Display;
				}
			}

			.group_4 {
				margin-top: 52rpx;
				.text_8 {
					margin-top: 8rpx;
					color: #999;
					font-size: 24rpx;
					font-family: SF Pro Display;
				}

				.equal-division {
					margin-top: 32rpx;

					.equal-division-item {
						flex: 0.48;
						padding: 20rpx 0;
						background-color: #f6f6f6;
						border-radius: 8rpx;
						.text_9 {
							color: #999;
							font-size: 28rpx;
							font-family: SF Pro Display;
						}
					}
					.current{
						border: solid 2rpx #1a7b99;
						.text_9{
							color: #1a7b99;
						}
					}
				}

				.text_11 {
					margin-top: 32rpx;
				}

				.group_5 {
					margin-top: 16rpx;
					.protocal {
						padding: 20rpx 0;
						flex: 0.48;
						background-color: #f6f6f6;
						border-radius: 8rpx;
						color: #999999;
						text-align: center;
					}
					.active{
						border: solid 2rpx #1a7b99;
						color: #1a7b99;
					}
				}
			}

			.group_6 {
				margin-top: 48rpx;
				.group_7 {
					margin-top: 32rpx;
					padding: 8rpx 0;
					border-bottom: 2rpx solid #7F7F7F;;
					input{
						flex:1;	
					}
					.text_15 {
						color: #515151;
					}
				}
				.text_16 {
					margin-top: 24rpx;
					color: #777e90;
				}
			}

			.font_3 {
				font-size: 32rpx;
				font-family: SF Pro Display;
				color: #000000;
			}

			.font_2 {
				font-size: 36rpx;
				font-family: SF Pro Display;
			}

			.font_4 {
				font-size: 28rpx;
				font-family: SF Pro Display;
				color: #000000;
			}

			.text-wrapper_4 {
				margin-top: 80rpx;
				padding: 24rpx 0;
				background-color: #1a7b99;
				border-radius: 8rpx;
				color: #ffffff;
				font-weight: 700;
				text-align: center;
			}
		}

		.font {
			font-size: 32rpx;
			font-family: SF Pro Display;
			color: #000000;
		}

		.image_4 {
			width: 40rpx;
			height: 40rpx;
			margin-bottom: 6rpx;
		}
	}
</style>